package yyy.ab.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.map.LinkedMap;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import yyy.ab.common.core.ResultBean;
import yyy.ab.modules.system.entity.SysMenu;
import yyy.ab.modules.system.service.SysMenuService;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Api(tags = "系统：菜单管理")
@RequestMapping("/system/menu")
public class SysMenuController {

    private final SysMenuService menuService;

    /**
     * 菜单页面
     */
    @RequestMapping
    @PreAuthorize("@ab.check('menu:list')")
    public ModelAndView jump() {
        ModelAndView view = new ModelAndView();
        view.setViewName("system/menu/index");
        return view;
    }

    @ApiOperation("查询应用模块")
    @GetMapping("/findList")
    public Object queryList() {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<SysMenu>();
        queryWrapper.orderByAsc("sort");
        List<SysMenu> list = menuService.list(queryWrapper);
        return list;
    }

    @ApiOperation("创建菜单")
    @PostMapping
    public Object create(@RequestBody SysMenu menu) {
        ResultBean resultBean = menuService.create(menu);
        return resultBean;
    }

    @ApiOperation("修改菜单")
    @PutMapping
    @PreAuthorize("@ab.check('menu:edit')")
    public Object update(@RequestBody SysMenu menu) {
        ResultBean resultBean = menuService.update(menu);
        return resultBean;
    }

    @ApiOperation("删除菜单")
    @DeleteMapping
    public Object deleteAll(@RequestBody Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        ResultBean resultBean = menuService.deleteAll(idList);
        return resultBean;
    }

    @ApiOperation("获取前端所需菜单")
    @GetMapping(value = "/build")
    public ResponseEntity<Object> buildMenus() {
        List<SysMenu> menuDtoList = menuService.findByUser(0L);
        List<SysMenu> menuDtos = menuService.buildTree(menuDtoList);
        LinkedMap<String, Object> result = new LinkedMap<>();
        result.put("homeInfo", getHomeInfo());
        result.put("logoInfo", getLogoInfo());
        result.put("menuInfo", menuService.buildMenus(menuDtos));
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    private Map<String, Object> getHomeInfo() {
        Map<String, Object> map = new HashMap<>();
        map.put("title", "首页");
        map.put("href", "welcome");
        return map;
    }

    private Map<String, Object> getLogoInfo() {
        Map<String, Object> map = new HashMap<>();
        map.put("title", "AB Platform");
        map.put("image", "/ab-ui/images/logo.png");
        map.put("href", "");
        return map;
    }
}
